package com.ares.aler.service.impl;

import com.ares.aler.entiy.NavPo;
import com.ares.aler.mapper.NavPoMapper;
import com.ares.aler.service.NavDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NavDaoImpl implements NavDao {
    @Autowired
    NavPoMapper navMapperDao ;
    @Override
    public List<NavPo> selectall() {
        return navMapperDao.selectAll();
    }

    // 将导航栏拼成树形数据结构，然后返回
    @Override
    public List<Map<String, Object>> navTree() {
        List<NavPo> listPo = navMapperDao.selectByPid("0");
        List<Map<String, Object>> firstList = new ArrayList<>();

        for(int i=0;i<listPo.size();i++){
            NavPo navPo = listPo.get(i);
            Map<String,Object> firstMap = new HashMap<>();
            firstMap.put("navPo",navPo);
            firstList.add(firstMap);
        }

        for(Map map1 : firstList){
            NavPo navPo = (NavPo) map1.get("navPo");
            String pid = navPo.getId();
            //获取二级菜单
            List<Map<String, Object>>  secondList = new ArrayList<>();
            Map<String,Object> secondMap = new HashMap<>();
            List<NavPo> secondPo = navMapperDao.selectByPid(pid);
            secondMap.put("secondPo",secondPo);
            secondList.add(secondMap);
            map1.put("second", secondList);
        }

        return firstList;
    }
}
